<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.dataName"
        placeholder="资料名称"
        style="width: 40%"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-model="listQuery.content"
        placeholder="操作内容"
        style="width: 15%"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-model="listQuery.operator"
        placeholder="操作人员"
        style="width: 15%"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
        style="margin-left: 10%"
      >
        搜索
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="序号" width="95">
        <template slot-scope="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>
      <el-table-column label="操作内容" align="center">
        <template slot-scope="scope">
          {{ scope.row.description}}
        </template>
      </el-table-column>
      <el-table-column label="操作人员" width="110" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.username }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作时间" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.operationTime }}
        </template>
      </el-table-column>
      <el-table-column
        class-name="status-col"
        label="操作类型"
        width="110"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.type }}
        </template>
      </el-table-column>
      <el-table-column
        class-name="status-col"
        label="模块"
        width="110"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.model }}
        </template>
      </el-table-column>
      
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

   
  </div>
</template>

<script>
import { fetchList ,fetchDataLogList} from '@/api/table'
import { createArticle, deletArticle, updateArticle } from '@/api/article'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'

export default {
  directives: { waves },
  components: { Pagination },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      total: 0,
      dialogFormVisible: false,
      dialogStatus: '',
      sortOptions: [
        { label: 'ID Ascending', key: '+id' },
        { label: 'ID Descending', key: '-id' },
      ],
      rules: {
        type: [
          { required: true, message: 'type is required', trigger: 'change' },
        ],
        timestamp: [
          {
            type: 'date',
            required: true,
            message: 'timestamp is required',
            trigger: 'change'
          }
        ],
        title: [
          { required: true, message: 'title is required', trigger: 'blur' },
        ]
      },
      textMap: {
        update: '编辑',
        create: '新建',
      },
      temp: {
        id: '',
        userName: '',
        type: '',
        operationTime: '',
        path: '',
        title: '',
        outline: '',
        stutas: ''
        // timestamp: new Date()
      },
      listQuery: {
        page: 1,
        limit: 20,
        username: undefined,
        model: undefined,
        type: undefined,
        sort: '+id'
      },
      statusOptions: [
        '专著',
        '期刊',
        '图书',
        '报纸',
        '会议文献',
        '科技报告',
        '标准文献',
        '专利文献',
        '学位论文',
        '政府出版物'
      ]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchDataLogList(this.listQuery).then((response) => {
        this.list = response.data.records
        this.total = response.data.total

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      console.log(this.listQuery)
      this.getList()
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    resetTemp() {
      this.temp = {
        dataName: '',
        dataType: '',
        author: '',
        path: '',
        title: '',
        outline: '',
        stutas: ''
        // timestamp: new Date()
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          // this.temp.id = parseInt(Math.random() * 100) + 1024 // mock a id
          // this.temp.author = 'vue-element-admin'
          // this.temp.timestamp = +new Date(this.temp.timestamp)
          createArticle(this.temp).then(() => {
            this.list.unshift(this.temp)
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000,
            })
            location.reload()
          })
        }
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          // tempData.timestamp = +new Date(tempData.timestamp) // change Thu Nov 30 2017 16:41:05 GMT+0800 (CST) to 1512031311464
          updateArticle(tempData).then(() => {
            const index = this.list.findIndex((v) => v.id === this.temp.id)
            this.list.splice(index, 1, this.temp)
            // console.log(this.list)
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
            location.reload()
          })
        }
      })
    },
    handleDelete(row, index) {
      
      deletArticle(row.id).then(() => {
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 2000
        })
        const index = this.list.findIndex((v) => v.id === this.temp.id)
        this.list.splice(index, 1)
      })
    },
  },
}
</script>
